﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using iver.Entity;
using DBEntity;

namespace iver.Controllers
{
    public class InfoTypeController : ApiController
    {
        /// <summary>
        /// 得到信息类型列表
        /// </summary>
        /// <returns>jsonstr</returns>
        [HttpPost]
        public ResResult InfoTypeList([FromBody]ReqList req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            var list = (from u in db.apInfoType
                        orderby u.orderNum descending
                        select u);
            var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }
        /// <summary>
        /// 得到信息类型列表
        /// </summary>
        /// <returns>jsonstr</returns>
        [HttpPost]
        public ResResult InfoTypeListByQuery([FromBody]ReqList req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            var list = (from u in db.apInfoType
                        join temp in db.apInfoType on u.fid equals temp.id into temp1
                        from ut in temp1.DefaultIfEmpty()
                        orderby u.orderNum descending
                        select new
                        {
                            u.id,
                            u.infoTypeName,
                            u.displayName,
                            u.icon,
                            u.treeIds,
                            u.orderNum,
                            u.isEnable,
                            u.fid,
                            u.rank,
                            u.type,
                            u.href,
                            u.remark,
                            fName = ut.infoTypeName,
                            fdName = ut.displayName,
                        });
            var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }
        /// <summary>
        /// 添加信息类型
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult InfoTypeAdd([FromBody]apInfoType req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            db.apInfoType.Add(req);
            db.SaveChanges();
            Help.Log(authInfo, req.id.ToString());
            Help.StaticApInfoType();
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessAdd,
                result = new
                {
                    id = req.id,
                },
            };
            //string mname = System.Reflection.MethodBase.GetCurrentMethod().Name.ToLower();
        }
        /// <summary>
        /// 编辑信息类型
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult InfoTypeEdit([FromBody]apInfoType req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            apInfoType model = db.apInfoType.FirstOrDefault(u => u.id == req.id);
            if (model == null)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            model.infoTypeName = req.infoTypeName == null ? model.infoTypeName : req.infoTypeName;
            model.displayName = req.displayName == null ? model.displayName : req.displayName;
            model.icon = req.icon == null ? model.icon : req.icon;
            model.treeIds = req.treeIds == null ? model.treeIds : req.treeIds;
            model.orderNum = req.orderNum == null ? model.orderNum : req.orderNum;
            model.isEnable = req.isEnable == null ? model.isEnable : req.isEnable;
            model.fid = req.fid == null ? model.fid : req.fid;
            model.rank = req.rank == null ? model.rank : req.rank;
            model.type = req.type == null ? model.type : req.type;
            model.href = req.href == null ? model.href : req.href;
            model.remark = req.remark == null ? model.remark : req.remark;
            int result = db.SaveChanges();
            Help.Log(authInfo, req.id.ToString());
            Help.StaticApInfoType();
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessEdit,
                result = new
                {
                    changeCount = result,
                },
            };
        }
        /// <summary>
        /// 删除信息类型
        /// </summary>
        /// <param name="req">id数组</param>
        /// <returns></returns>
        [HttpPost]
        public ResResult InfoTypeDel([FromBody]ReqID<List<int>> req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            var list = db.apInfoType.Where(u => req.id.Contains(u.id));
            int delCount = list.Count();
            if (delCount == 0)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            db.apInfoType.RemoveRange(list);
            db.SaveChanges();
            Help.Log(authInfo, Newtonsoft.Json.JsonConvert.SerializeObject(req.id));
            Help.StaticApInfoType();
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessDel,
                result = new
                {
                    delCount,
                },
            };
        }
    }
}